# -*- coding: utf-8 -*-
from .Monitor import Monitor

import ctypes
from ctypes import windll
SysColour = windll.user32.GetSysColor(1)

class RECT(ctypes.Structure):
	"""
	Rectangle structure for EnumDisplayMonitors call
	"""
	_fields_ = [
		('left', ctypes.c_long),
		('top', ctypes.c_long),
		('right', ctypes.c_long),
		('bottom', ctypes.c_long)
		]
	def dump(self):
		f = (self.left, self.top, self.right, self.bottom)
		return [int(i) for i in f]

class MONITORINFO(ctypes.Structure):
	"""
	Monitor structure for EnumDisplayMonitors call
	"""
	_fields_ = [
		('cbSize', ctypes.c_ulong),
		('rcMonitor', RECT),
		('rcWork', RECT),
		('dwFlags', ctypes.c_ulong)
		]

def findMonitors():
	"""
	Get the monitors connected too this machine
	"""
	retval = []
	CBFUNC = ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_ulong, ctypes.c_ulong, ctypes.POINTER(RECT), ctypes.c_double)
	def cb(hMonitor, hdcMonitor, lprcMonitor, dwData):
		r = lprcMonitor.contents
		data = [hMonitor]
		data.append(r.dump())
		retval.append(data)
		return 1
	cbfunc = CBFUNC(cb)
	temp = windll.user32.EnumDisplayMonitors(0, 0, cbfunc, 0)
	return retval

def getMonitors(*_args, **_kwargs):
	retval = []
	for i, (hMonitor, extents) in enumerate(findMonitors()):
		mi = MONITORINFO()
		mi.cbSize = ctypes.sizeof(MONITORINFO)
		mi.rcMonitor = RECT()
		mi.rcWork = RECT()			
		res = windll.user32.GetMonitorInfoA(hMonitor, ctypes.byref(mi))
		data = Monitor(hMonitor, mi.rcMonitor.dump(), mi.rcWork.dump(), mi.dwFlags, i)
		data.setBlendColour(SysColour)
		retval.append(data)
	return retval
